package org.pausd.gunn.compsci;

import java.util.ArrayList;
/**
 * The server-side Game class, handles logic
 * @author CodyS
 *
 */
public class LocalGame extends Game {
	private int counter;
	/**
	 * The index of the current player
	 */
	private int currentTurn=0;
	/**
	 * is the game playing?
	 */
	private boolean playing;
	private ArrayList<Player> unreadyPlayers = new ArrayList<Player>();
	private ArrayList<Player> readyPlayers = new ArrayList<Player>();
	@Override
	public void addToCounter(int add) {
		counter += add;
		
		turn();
	}
	public void addPlayer(Player p) {
		if(playing) {}
		else {
			unreadyPlayers.add(p);
			p.ready();
		}
	}
	@Override
	public void playerReady(Player p) {
		unreadyPlayers.remove(p);
		readyPlayers.add(p);
		if(unreadyPlayers.size()<1) {
			playing = true;
			turn();
		}
	}
	/**
	 * Start a turn, tell all the players waht the counter is, then tell the current player
	 * to do their turn.
	 */
	protected void turn() {
		for(Player x : readyPlayers) {
			x.counter(counter);
		}
		currentTurn = currentTurn % readyPlayers.size();
		readyPlayers.get(currentTurn).doTurn();
		currentTurn++;
	}
	@Override
	public void removePlayer(Player p) {
		unreadyPlayers.remove(p);
		readyPlayers.remove(p);
	}
}
